package com.engine.budget.cmd;

import com.engine.budget.enums.VerifyMessage;
import com.engine.budget.manager.InstructionManager;
import com.engine.budget.util.VerifyUtil;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author DY
 * @version 1.0
 * @date 2024/7/8 9:31
 * @description: 预算仅执行指令
 */
public class OnlyExecBudgetCmd extends AbstractCommonCommand<Map<String,Object>> {

    private Map<String, Object> otherMap;

    private String logId;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {

        //返回数据
        Map<String, Object> result = new HashMap();

        try {
            this.logId = Util.null2String(otherMap.get("logId"));
            result.put("logId", logId);
            logger.info("外部系统调用预算仅执行指令接口cmd_start...LogID:"+logId);

            //业务参数
            JSONArray paramsArr = JSONArray.fromObject(params.get("params"));
            //返回数据数组
            JSONArray resArray = new JSONArray();
            //验证失败的表示
            boolean errFlag = false;

            RecordSet rs = new RecordSet();

            for (int i = 0; i < paramsArr.size(); i++) {

                JSONObject data = paramsArr.getJSONObject(i);

                String instruction = Util.null2String(data.getOrDefault("instruction", ""));

                Map<String, Object> checkMap = VerifyMessage.SUCCESS.fromMap();

                if(!"JZX".equals(instruction)) {
                    checkMap = VerifyMessage.YWYZ_ERR.setErrorMessage("【instruction_指令】参数不合法").fromMap();
                    checkMap.put("status", -1);
                    checkMap.put("data", data);
                    resArray.add(checkMap);
                    continue;
                }

                //参数必填验证
                Map<String, String> emptyMap = VerifyUtil.paramMustVeifty(data);

                if(emptyMap.size() > 0) {
                    String emptyErrMessage = VerifyUtil.getEmptyErrMessage(emptyMap);
                    checkMap = VerifyMessage.MUST_EMPTY.setErrorMessage(emptyErrMessage).fromMap();
                    checkMap.put("status", -1);
                    checkMap.put("data", data);
                    errFlag = true;
                }
                resArray.add(checkMap);
            }

            if (errFlag) {
                result.put("result", resArray);
                return result;
            }

            JSONArray newResArray = new JSONArray();
            for (int i = 0; i < paramsArr.size(); i++) {
                JSONObject data = paramsArr.getJSONObject(i);
                JSONObject yzData = resArray.getJSONObject(i);
                newResArray.add(yzData);

                InstructionManager.addInstructionLog(data, yzData, rs);
            }

            result.put("result", newResArray);
            return result;
        }catch (Exception e) {
            Map<String, Object> errMap = VerifyMessage.XT_ERR.fromMap();
            errMap.put("status", -3);
            result.put("result", "[]");
            result.putAll(errMap);
            logger.error("外部系统调用预算仅执行指令接口cmd——异常...err:"+e.getMessage(), e);
        }

        logger.info("外部系统调用预算仅执行指令接口cmd_end...LogID:"+logId);
        return result;
    }
    
    public OnlyExecBudgetCmd(Map<String, Object> params, Map<String, Object> otherMap) {
        this.params = params;
        this.otherMap = otherMap;
        this.otherMap.put("user",user);
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }
}
